/*
 * Name: EventsData
 * Author: Sam Oakley
 * Date: 1/4/2010
 * Version Number: 1.2
 * Description: Very important! This is where the app checks for new events. Was initially
 * in 'Upcoming' but moved to here to become a scheduled service. 
 * By default runs every 1hr to check for new events.
 * Parsers the XML, adds any new events to the database.
 * If there have been changes to existing events, updates it.
 * Notification given to user.
 * Changes History: 
 * 1.0 Created class
 * 1.1 Added 'attending'
 * 1.2 Added 'tagline'
 */
package com.blork.rag;

import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.IBinder;
import android.preference.PreferenceManager;
import android.util.Log;

public class EventService extends Service implements Runnable {

	static final String ACTION_RAG_EVENT = "ACTION_RAG_EVENT"; 
	
	private NotificationManager nm;
	private Boolean notificationsEnabled;
	
	public void onCreate() {
		super.onCreate();
		String ns = Context.NOTIFICATION_SERVICE;
		nm = (NotificationManager) getSystemService(ns);
		
        SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(this);	
        notificationsEnabled = settings.getBoolean("notifications_enabled", true);

		Network network = new Network(this.getApplicationContext());
		
		if(!network.IsDataEnabled()){
			this.stopSelf();
		}

        //Check to see if the device is connected or connecting to the Internet
		if (network.IsNetworkConnected()) {
			if(notificationsEnabled){
				int icon = android.R.drawable.stat_notify_sync;
				Notification notification = new Notification(icon, "Checking for new events.", System.currentTimeMillis());
				Intent notificationIntent = new Intent(this, Upcoming.class);
				PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);
				notification.setLatestEventInfo(this, "Checking for new events.", "Checking for new Sheffield Rag events.", contentIntent);
				notification.flags = Notification.FLAG_ONGOING_EVENT^Notification.FLAG_NO_CLEAR;
				nm.notify(3, notification);
			}
	       	Log.d(Rag.TAG, "Running update.");
			Thread thread = new Thread(this);
	        thread.start(); 
		} else {
			Log.e(Rag.TAG, "Not connected to net.");
		}
	}
	
	
	public void run() {
	    try{
	    	//TODO: Why even make it a constructor? this should be a static method or something.
	    	new RagEvents(this.getApplicationContext(), nm, notificationsEnabled);
    	}catch (Exception e){
    		Log.e("Rag EventService",e.getMessage(),e);	
    	}finally{
    		Log.d("Rag EventService","Finished.");
    		sendBroadcast(new Intent(ACTION_RAG_EVENT));
    		
    		String ns = Context.NOTIFICATION_SERVICE;
    		NotificationManager nm2 = (NotificationManager) getSystemService(ns);
    		nm2.cancel(3);
    		stopSelf();
    	}
	}
	

	@Override
	public IBinder onBind(Intent arg0) {
		// TODO Auto-generated method stub
		return null;
	}
	
}
